﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;

public partial class Admin_DanhMuc_LoaiThe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind_Data();
        }
    }

    protected void Bind_Data()
    {
        int TotalRows = 0;
        TList<LoaiThe> TList_Catalog = DataRepository.LoaiTheProvider.GetPaged("", "Id asc", 0, 0, out TotalRows);
        gridCatalog.DataSource = TList_Catalog;
        gridCatalog.DataBind();
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            LoaiThe entThanhPho = new LoaiThe();
            entThanhPho.Id = Convert.ToInt32(H_CatalogId.Value);
            entThanhPho.TenLoaiThe = Common.StandardString(txtTen.Text);
            entThanhPho.GiaTien = Common.StandardString(txtGiaTien.Text);
            entThanhPho.TrangThai = chkSuDung.Checked;
            if (entThanhPho.Id == 0)
            {
                entThanhPho.Id = 0;
                updateStatus = DataRepository.LoaiTheProvider.Insert(entThanhPho);
            }
            else
            {
                updateStatus = DataRepository.LoaiTheProvider.Update(entThanhPho);
            }
            if (updateStatus)
            {
                Bind_Data();
                cmdReset_Click(sender, e);
            }
            else
                WebMessage.Show("Cập nhật Loại thẻ thất bại!");
        }
        catch (Exception ex)
        {
            Common.WriteLog("LoaiThe.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Loại thẻ thất bại!");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        H_CatalogId.Value = "0";
        chkSuDung.Checked = true;
        txtGiaTien.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                LoaiThe entLoaiThe = DataRepository.LoaiTheProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entLoaiThe != null)
                {
                    txtTen.Text = entLoaiThe.TenLoaiThe;
                    txtGiaTien.Text = entLoaiThe.GiaTien;
                    chkSuDung.Checked = entLoaiThe.TrangThai;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("LoaiThe.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdLoaiThe = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.LoaiTheProvider.Delete(IdLoaiThe);
                if (!deleteStatus)
                    WebMessage.Show("Xóa loại thẻ thất bại");
            }
            catch (Exception ex)
            {
                Common.WriteLog("LoaiThe.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                WebMessage.Show("Xóa loại thẻ thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data();
        txtTen.Text = "";
        txtGiaTien.Text = "";
        H_CatalogId.Value = "0";
        chkSuDung.Checked = false;
    }
}